package com.block.sms.service.send;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.db.entity.SmsConfigInfo;
import com.block.db.entity.SmsTemplate;
import com.block.sms.dto.SmsBalanceDTO;
import com.block.sms.dto.SmsResultDTO;
import com.block.sms.service.ISmsSendService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DanMiSmsSendServiceImpl implements ISmsSendService {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public SmsResultDTO sendSms(String mobile, String outId, SmsTemplate smsTemplate, SmsConfigInfo smsConfigInfo, JSONObject paramObject) {

        SmsResultDTO  resultDTO = new SmsResultDTO();
        try {
            JSONArray paramArr = paramObject.getJSONArray("paramArr");

            HttpRequest httpRequest = HttpUtil.createPost(smsConfigInfo.getUrl());

            String content = StrUtil.format(smsTemplate.getTemplateContext(), paramArr.stream().toArray());
            String timestamp = String.valueOf(System.currentTimeMillis());
            String sign = SecureUtil.md5(smsConfigInfo.getAccount() + smsConfigInfo.getSecret() + timestamp);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("respDataType", "JSON");
            paramMap.put("accountSid", smsConfigInfo.getAccount());
            paramMap.put("smsContent", content);
            paramMap.put("to", mobile);
//            paramMap.put("templateid", smsTemplate.getTemplateId());
//            paramMap.put("templateid", smsTemplate.getTemplateId());
            paramMap.put("timestamp", timestamp);
            paramMap.put("sig", sign);

            httpRequest.form(paramMap);

            String bodyResult = httpRequest.execute().body();
            log.info("【旦米短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                    smsConfigInfo.getUrl(), JSON.toJSONString(paramMap), bodyResult);

            JSONObject resultJson = JSON.parseObject(bodyResult);
            String respCode = resultJson.getString("respCode");// 【任信花】尊敬的用户，您的验证码214350如非本人操作，请忽略此短信。


            resultDTO.setData(bodyResult);
            if (StrUtil.equals("0000", respCode)){
                resultDTO.setCode(AppRspCodeConstant.SUCCESS);
                resultDTO.setHttpStatus(respCode);
                resultDTO.setRequestId(outId);
                resultDTO.setMessage(bodyResult);
                return resultDTO;
            }
            resultDTO.setMessage(bodyResult);
        } catch (Exception exception) {
            log.error(String.format("【旦米短信服务】发送发生异常【mobile】%s【code】%s", mobile, paramObject.toJSONString()), exception);
        }
        resultDTO.setCode(AppRspCodeConstant.OPERATION_FAILURE);
        return resultDTO;
    }

    @Override
    public SmsBalanceDTO queryBalanceInfo(SmsConfigInfo smsConfigInfo) {

        SmsBalanceDTO smsBalanceDTO = new SmsBalanceDTO();
        smsBalanceDTO.setBalanceAmount("0");
        try {
            HttpRequest httpRequest = HttpUtil.createPost(smsConfigInfo.getQueryUrl());

            String timestamp = String.valueOf(System.currentTimeMillis());
            String sign = SecureUtil.md5(smsConfigInfo.getAccount() + smsConfigInfo.getSecret() + timestamp);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("respDataType", "JSON");
            paramMap.put("accountSid", smsConfigInfo.getAccount());
            paramMap.put("timestamp", timestamp);
            paramMap.put("sig", sign);

            httpRequest.form(paramMap);

            String bodyResult = httpRequest.execute().body();
            log.info("【旦米短信服务】【余额查询】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                    smsConfigInfo.getUrl(), JSON.toJSONString(paramMap), bodyResult);

            JSONObject resultJson = JSON.parseObject(bodyResult);
            String respCode = resultJson.getString("respCode");// 【任信花】尊敬的用户，您的验证码214350如非本人操作，请忽略此短信。

            if (StrUtil.equals("0000", respCode)){
                String balance = resultJson.getString("balance");
                smsBalanceDTO.setBalanceAmount(balance);
            }
            return smsBalanceDTO;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return smsBalanceDTO;
    }

}
